package d0906;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Random;

public class Arrays类 {
    class A{}

    public static void main(String[] args) {

        Integer[] arr1 = { 3, 5, 6, 72, 44, 22, 90 };

        Arrays.sort(arr1);
        System.out.println(arr1);
        System.out.println(Arrays.toString(arr1));
        // 匿名内部类
        Comparator<Integer> comp = new Comparator<Integer>() {
            // 返回值整数, 表示 o1>o2, 负数反之,  0相等
            @Override
            public int compare(Integer o1, Integer o2) {
                return o2 - o1;
            }
        };
        Arrays.sort(arr1, comp);
        System.out.println(Arrays.toString(arr1));


        Student[] stus = new Student[10];
        for (int i = 0; i < 10; i++) {
            stus[i] = new Student();
        }
        // 排序前获取第一个学生
        Student s1 = stus[0];

        // 被排序的数组元素, 必须实现 Comparable 接口
        Arrays.sort(stus);
        System.out.println(Arrays.toString(stus));

        // fill 填充
        String[] arr2 = new String[10];
        Arrays.fill(arr2, "李四");
        System.out.println(Arrays.toString(arr2));

        //binarySearch(a, key)  // 折半查找, 必须在排序后使用
        Arrays.sort(arr1);
        int index = Arrays.binarySearch(arr1, 44);
        System.out.println("44位于 : " + index);

        index = Arrays.binarySearch(stus, s1);
        System.out.println(s1 + "位于 : " + index);

    }

    private static int count;
    private static Random random = new Random();// 随机对象
    static class Student implements Comparable<Student>{
        private String name;
        private int age;
        Student(){
            name = "张三" + ++count;
            age = random.nextInt(140);
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", age=" + age + "]";
        }
        @Override
        public int compareTo(Student o) {
            return this.age - o.age;
        }
    }
}
